import Vue from 'vue'
import App from './App.vue'
// 引入mui
import '../node_modules/mui/css/mui.min.css'
import '../node_modules/mui/css/icons-extra.css'
// 引入mint-ui组件库
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUI)

// 导入resource模块
import resource from 'vue-resource'
Vue.use(resource)
    // 设置发送ajax请求的根路径
Vue.http.options.root = 'http://www.liulongbin.top:3005';
// 设置post请求体的格式
Vue.http.options.emulateJSON = true;

Vue.config.productionTip = false

// 导入路由模块
import router from './router.js'

import moment from 'moment'
// 定义区全局过滤器
Vue.filter('timeF', function(timeStr, pattern = "YYYY-MM-DD HH:mm:ss") {
    return moment(timeStr).format(pattern)
})


// 引入缩略图插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

// 导入vuex
import Vuex from 'vuex'
Vue.use(Vuex)


// 从本地存储中获取商品数量
var car = JSON.parse(localStorage.getItem('car') || '[]')
const store = new Vuex.Store({
    state: {
        car: car
            // {id:'商品的id',price:'商品价格',count:'商品的数量',selected:'是否选中'},
    },
    mutations: {
        // 添加到购物车，把信息保存到state中的car里面
        addtoCar(state, list) {
            let flag = true
            state.car.some(item => {
                if (item.id == list.id) {
                    item.count = item.count + parseInt(list.count);
                    flag = false
                    return true
                }
            })
            if (flag) {
                state.car.push(list)
            }
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updateGoodsInfo(state, goodsInfo) {
            state.car.some(item => {
                if (item.id == goodsInfo.id) {
                    item.count = parseInt(goodsInfo.count)
                    return true
                }
            })
            localStorage.setItem("car", JSON.stringify(state.car))
        },
        removeFromCar(state, id) {
            state.car.some((item, i) => {
                if (item.id == id) {
                    state.car.splice(i, 1)
                    return true
                }
            })
            localStorage.setItem("car", JSON.stringify(state.car))
        },
        updateGoodsSlected(state, info) {
            state.car.some(item => {
                if (item.id == info.id) {
                    item.selected = info.selected
                }
            })
            localStorage.setItem("car", JSON.stringify(state.car))
        }
    },
    getters: {
        // 获取当前购物车中商品的总数
        getAllCount(state) {
            var count = 0;
            state.car.forEach(item => {
                count += item.count
            })
            return count
        },
        //将商品的数量分别同步到数字输入框中所需要的数据
        goodsCount(state) {
            var o = {}
            state.car.forEach(item => {
                o[item.id] = item.count
            })
            return o
        },
        getGoodsSelected(state) {
            var o = {}
            state.car.forEach(item => {
                o[item.id] = item.selected
            })
            return o
        },
        getgoodsInfo(state) {
            var o = {
                count: 0,
                amount: 0
            }
            state.car.forEach(item => {
                if (item.selected) {
                    console.log(item);

                    o.count += item.count
                    o.amount += item.price * item.count
                }
            })
            return o
        }
    }
})


new Vue({
    render: h => h(App),
    router,
    store
}).$mount('#app')